#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{




    ui->setupUi(this);
    this->setWindowTitle("WiFi智能摄像头");
    QIcon icon(":/camera.png");
    this->setWindowIcon(icon);
     QIcon icon1(":/pig.png");
     QIcon icon2(":/cat.png");
     QIcon icon3(":/dog.png");
     QIcon icon4(":/rabbit.png");
     ui->upbutton->setIconSize(QSize(20,20));
     ui->upbutton->setIcon(icon1);

     ui->downbutton->setIconSize(QSize(20,20));
     ui->downbutton->setIcon(icon2);


     ui->leftbutton->setIconSize(QSize(20,20));
     ui->leftbutton->setIcon(icon3);

     ui->rightpshbutton->setIconSize(QSize(20,20));
     ui->rightpshbutton->setIcon(icon4);
    tsocket=new QTcpSocket;
    tsocket->connectToHost("47.121.24.16",8000);
    connect(tsocket,&QTcpSocket::connected,[]()
    {

        qDebug()<<"连接到服务器";


    });

    connect(tsocket,&QTcpSocket::readyRead,this,&Widget::tcpread);


      QJsonObject obj;
      obj.insert("cmd","get_video_data");
      obj.insert("appid","1000");
      obj.insert("deviceid","0001");
      tsocket->write(QJsonDocument(obj).toJson());//app上线请求



}

Widget::~Widget()
{
    delete ui;
}
void Widget::tcpread()
{
    //读取服务器端口号

     //{"cmd":"return_port","port":9001};
    QByteArray ba=tsocket->readAll();
    //QJsonObject obj=QJsonDocument(ba).object();
    QJsonDocument jsonDoc = QJsonDocument::fromJson(ba);
    QJsonObject obj=jsonDoc.object();
    if(obj.value("cmd").toString()=="return_port")
    {

         port=obj.value("port").toInt();
         //app收到端口号，创建udp
         usocket=new QUdpSocket;
         //向服务器发送无关信息为了标记,让服务器那边udp知道app的struct sockaddr_in;
         usocket->writeDatagram("hello",QHostAddress("47.121.24.16"),port);
         connect(usocket,&QUdpSocket::readyRead,this,&Widget::udpread);//调用收数据



    }
    else
    {
          qDebug()<<"接收到错误信息";

    }


}

 void Widget::udpread()
 {
      qDebug()<<"接收视频数据";
     QByteArray ba;
     ba.resize(usocket->pendingDatagramSize());
     usocket->readDatagram(ba.data(),ba.size());
     QPixmap pix;
     //ui->label->loadfromdata();
     pix.loadFromData(ba);
     ui->label->setPixmap(pix);

 }



void Widget::on_upbutton_clicked()
{
    QJsonObject obj;
    //{"cmd":"control","action":"left","appid":"1000","deviceid":"0001"}
    obj.insert("cmd","control");
    obj.insert("action","up");
    obj.insert("deviceid","0001");
    tsocket->write(QJsonDocument(obj).toJson());

}

void Widget::on_downbutton_clicked()
{
    QJsonObject obj;
    //{"cmd":"control","action":"left","appid":"1000","deviceid":"0001"}
    obj.insert("cmd","control");
    obj.insert("action","down");
    obj.insert("deviceid","0001");
    tsocket->write(QJsonDocument(obj).toJson());
}

void Widget::on_leftbutton_clicked()
{
    QJsonObject obj;
    //{"cmd":"control","action":"left","appid":"1000","deviceid":"0001"}
    obj.insert("cmd","control");
    obj.insert("action","left");
    obj.insert("deviceid","0001");
    tsocket->write(QJsonDocument(obj).toJson());
}



void Widget::on_rightpshbutton_clicked()
{
    QJsonObject obj;
    //{"cmd":"control","action":"left","appid":"1000","deviceid":"0001"}
    obj.insert("cmd","control");
    obj.insert("action","right");
    obj.insert("deviceid","0001");
    tsocket->write(QJsonDocument(obj).toJson());
}
